import * as step from '@flow-step/step-toolkit'
import {RosInterface} from './ros'
import {ValidateTemplateRequest, ValidateTemplateResponse} from '@alicloud/ros20190910'
import {IParams} from './params'
import {getTemplateBody} from './getTemplate'
import {isNotEmpty} from './util'


export async function validateTemplate(params: IParams, ros: RosInterface) {
    const templateBody = getTemplateBody()
    const request = new ValidateTemplateRequest({
        regionId: params.REGION_ID,
        templateBody: templateBody
    })
    try {
        await ros.validateTemplate(request)
    } catch (error: any) {
        step.error(`Failed to ValidateTemplate. Code: ${error?.code}, Message: ${error.data?.Message}, RequestId: ${error.data?.RequestId}`);
        if (!isNotEmpty(error.data?.Message)) {
            step.error(`Unexpected ValidateTemplate error. Error: ${JSON.stringify(error, null, 2)}`);
        }
        return false
    }
    return true
}